package com.example.demo.controller;

import com.example.demo.domain.Message;
import com.example.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: demo
 * @description: 系统通知的控制类
 * @author: wangze
 * @create: 2020-04-27 10:21
 **/
@RestController
@RequestMapping
@CrossOrigin
public class MessageController {
    @Autowired
    private MessageService messageService;

    /**
     * @Description: 根据职工工号获取他的所有消息
     * @Param: worknumber:职工工号
     * @return: 消息列表
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('users','users:permissions:basic')")
    @GetMapping(value = "/messages")
    public List<Message> getAllMessages(@RequestParam("worknumber") String worknumber){
        return messageService.findByWorkNumber(worknumber);
    }

    /**
     * @Description: 根据ID返回一个Message
     * @Param:  id: 消息ID
     * @return: 消息实体
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('users','users:permissions:basic')")
    @GetMapping(value = "/message")
    public Message getMessageById(@RequestParam("id") Integer id){
        return messageService.findMessageById(id);
    }

    /**
     * @Description: 获取未读消息总数
     * @Param:  worknumber: 职工工号
     * @return:  未读消息数量
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('users','users:permissions:basic')")
    @GetMapping(value = "/message/quantity")
    public int getTotal(@RequestParam("worknumber") String worknumber){
        return messageService.getTotal(worknumber);
    }

    /**
     * @Description: 设置消息状态为已读
     * @Param:  id: 消息ID
     * @return:  消息实体
     * @Author: wangze
     */
    @PreAuthorize("hasPermission('users','users:permissions:basic')")
    @GetMapping(value = "/after-read")
    public Message setState(@RequestParam("id") Integer id){
        return messageService.setState(id);
    }
}
